<template>
  <div class="app-container"
       v-loading.fullscreen.lock="allLoading"
       element-loading-text="拼命处理中,请耐心等待"
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-dialog :close-on-click-modal="changeBitFlagIsNone" :title="mainDialogInfo.title" :visible.sync="showDialog"
               width="90%" append-to-body @close="closeDialog">
      <el-form ref="form" :disabled="changeBitFlagIsNone" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="name" label="名称">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="description" label="描述">
              <el-input v-model="form.description" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item prop="api_base" label="API链接地址">
            <el-input v-model="form.api_base" rows="2"  type="textarea" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="api_key" label="API key">
            <el-input v-model="form.api_key"   type="text" placeholder="请输入"/>
          </el-form-item>
          <el-form-item prop="api_token" label="API token">
            <el-input v-model="form.api_token"   type="text" placeholder="请输入" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-switch
                v-model="form.status"
                active-color="#ff4949"
                inactive-color="#13ce66"
                active-value="disabled"
                inactive-value="enabled"
                active-text="禁用"
                inactive-text="启用">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="系统创建" prop="is_system">
              <el-switch
                v-model="form.is_system"
                active-color="#ff4949"
                inactive-color="#13ce66"
                active-value="NO"
                inactive-value="YES"
                active-text="否"
                inactive-text="是">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="created_at" label="创建时间">
              <el-date-picker v-model="form.created_at" type="datetime" placeholder="选择创建时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="updated_at" label="更新时间">
              <el-date-picker v-model="form.updated_at" type="datetime" placeholder="选择更新时间" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <template v-if="!changeBitFlagIsNone">
          <el-button type="primary" @click="submitMainDialog">确 定</el-button>
        </template>
        <el-button @click="cancelMainDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { sksDialogFormMixin } from 'sks-plugin-el-erp/lib/dialog-form'
import { addAIApi, getAIApi,  updateAIApi } from '@/api/ai'

export default {
  name: "AddAIApiDialog",
  components: {
  },
  mixins: [
    sksDialogFormMixin,
  ],
  data() {
    return {
      showEmojiDialog: false,
      defaultForm: {
        id: undefined,
        name: undefined,
        description: undefined,
        api_base: undefined,
        api_key: undefined,
        api_token: undefined,
        is_system: undefined,
        status: undefined,
        created_at: undefined,
        updated_at: undefined,
      },
      defaultFormNotNull: {
        virtualDocumentList: [],
      },

      rules: {
        name: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        description: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        api_base: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        api_key: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
      },
    };
  },
  methods:{
    showByUniqueId({uniqueId,extra,dialogInfo,virtualChangeBitFlag=this.sksUtils.CHANGE_BIT_FLAG.none}){
      getAIApi({id:uniqueId}).then(res=>{
        console.log(res)
        let data=res.data
        this.show({
          extra: extra,
          form: this.sksUtils.mergeFromObject({},data,{
            virtualChangeBitFlag: virtualChangeBitFlag,
          }),
          dialogInfo: dialogInfo
        })
      })
    },
    validateSuccess() {
      let data = this.sksUtils.mergeFromObject({}, this.form);

      if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallback)) {
        this.mainDialogInfo.submitCallback({submitData: data, ref: this})
        //this.showDialog=false
        return
      }
      switch(data.virtualChangeBitFlag) {
        case this.sksUtils.CHANGE_BIT_FLAG.add:
          //此处换成新增接口 start
          addAIApi(data).then(res=>{
            this.sksUtils.msgSuccess("新增成功");
            this.closeDialog()
            if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallbackSuccess)){
              this.mainDialogInfo.submitCallbackSuccess({resData: res.data})
            }
          })
          //此处换成新增接口 end
          break;
        case this.sksUtils.CHANGE_BIT_FLAG.modify:
          updateAIApi(data).then(res=>{
            this.sksUtils.msgSuccess("修改成功");
            this.closeDialog()
            if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallbackSuccess)){
              this.mainDialogInfo.submitCallbackSuccess({resData: data})
            }
          })
          break;
        default:
          this.sksUtils.msgError("未知的变更标识");
          break;
      }
    },
  }
}
</script>

<style scoped>
</style>
